<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/5/3
 * Time: 14:06
 */
namespace app\first\model;

use think\Model;

class Friend extends Model{
    protected $autoWriteTimestamp = true;
    /**
     * @param array $where
     * @param string $order
     * @param int $limit
     * @param string $filed
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($where=[],$order='create_time desc',$limit=10,$filed='*'){
        return $this
            ->field($filed)
            ->with(['user'=>function($query){
                $query->field('id,username,nickname,sex,head,rong_token');
            }])
            ->where($where)
            ->order($order)
            ->paginate($limit);
    }

    /**
     * @param int $id
     * @return array|\PDOStatement|string|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getOneById($id=0){
        $info = $this->find($id);
        $info->hits = $info['hits'] + 1;
        $info->save();
        return $this
            ->with(['user'=>function($query){
                $query->field('id,username,nickname,sex,head,rong_token');
            }])
            ->where('id','eq',$id)
            ->find();
    }

    public function user(){
        return $this
            ->hasOne('member','id','mid');
    }

    public function addNew($mid,$rid,&$out=''){
        if(!$mid || !$rid){
            $out = '缺少参数';
            return false;
        }
        $count = $this
            ->where('mid','eq',$mid)
            ->where('rid','eq',$rid)
            ->count();
        if($count){
            $out='已经是你的好友了';
            return false;
        }
        $data = [
            'mid'=>$mid,
            'rid'=>$rid
        ];
        if(!$this->save($data)){
            $out = '添加失败';
            return false;
        }
        return true;
    }

    /**
     * @param $id
     * @param $type
     * @param $info
     * @param string $out
     * @return bool
     */
    public function check($id,$type,$info,&$out=''){
        if(!$id || !$type){
            $out = '缺少参数';
            return false;
        }
        $_info = $this->find($id);
        if(!$_info){
            $out='申请不存在';
            return false;
        }
        if($type==3){
            if(!$info){
                $out = '填写不通过原因';
                return false;
            }
            $_info->info =$info;
        }
        $_info->status = $type;
        if(!$_info->save()){
            $out = '操作成功';
            return false;
        }
        return true;
    }

    public function black($mid,$uid,$type,&$out=''){
        if(!$mid || !$uid){
            $out = '缺少参数';
            return false;
        }
        $info = $this
            ->where('mid','eq',$mid)
            ->where('rid','eq',$uid)
            ->find();
        if(!$info){
            $out = '不是你的好友';
            return false;
        }
        if($type==1){
            $info->is_black = 1;
        }else if($type==2){
            $info->is_black = 0;
        }
        if($type==3){
            if(!$info->delete()){
                $out = '操作失败';
                return false;
            }
        }else{
            if(!$info->save()){
                $out = '操作失败';
                return false;
            }
        }
        return true;
    }

    public function follow($mid,$uid,$type=1,&$out=''){
        if(!$uid || !$type){
            $out = '缺少参数';
            return false;
        }
        $_info = $this->where('rid','eq',$mid)->find();
        if(!$_info){
            $out='不是您的好友';
            return false;
        }
        $_follow = explode(',',$_info['follow']);
        if($type==1){
            if(in_array($uid,$_follow)){

            }
        }
        $_info->follow = $type;
        if(!$_info->save()){
            $out = '操作成功';
            return false;
        }
        return true;
    }
}